<template>
  <div class="qrcode-login">
    <div class="qrcode-container">
      <div class="qrcode-box">
        <div class="scan-line"></div>
      </div>
      <p class="qrcode-tip">请使用APP扫码登录</p>
      <p class="qrcode-refresh">
        <span @click="refreshQrCode">刷新二维码</span>
      </p>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, onBeforeUnmount } from 'vue'

// 发送登录表单事件
const emit = defineEmits(['login'])

// 二维码状态
const qrcodeStatus = ref('pending') // pending, scanning, success, expired

// 模拟定期检查二维码状态
let checkInterval: number | null = null

onMounted(() => {
  startQrCodeCheck()
})

onBeforeUnmount(() => {
  if (checkInterval) {
    clearInterval(checkInterval)
  }
})

// 刷新二维码
const refreshQrCode = () => {
  console.log('刷新二维码')
  qrcodeStatus.value = 'pending'
  // 这里可以添加刷新二维码的实际逻辑

  // 重新开始检查
  startQrCodeCheck()
}

// 开始检查二维码状态
const startQrCodeCheck = () => {
  // 清除现有的检查
  if (checkInterval) {
    clearInterval(checkInterval)
  }

  // 设置新的检查间隔
  checkInterval = setInterval(() => {
    // 这里应该是从服务器查询二维码状态
    // 这里仅作为演示，随机模拟状态变化
    const random = Math.random()

    if (random < 0.1 && qrcodeStatus.value === 'pending') {
      qrcodeStatus.value = 'scanning'
      console.log('二维码被扫描')
    } else if (random < 0.05 && qrcodeStatus.value === 'scanning') {
      qrcodeStatus.value = 'success'
      console.log('扫码登录成功')

      // 登录成功，通知父组件
      emit('login', {
        type: 'qrcode',
        qrcodeId: 'mock-qrcode-id',
      })

      // 停止检查
      if (checkInterval) {
        clearInterval(checkInterval)
      }
    }
  }, 2000)
}
</script>

<style scoped>
.qrcode-login {
  width: 100%;
  padding: 20px 0;
}

.qrcode-container {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.qrcode-box {
  width: 180px;
  height: 180px;
  background: #fff;
  border-radius: 5px;
  position: relative;
  margin-bottom: 15px;
  overflow: hidden;
  /* 临时生成一个二维码的图案 */
  background-image: repeating-conic-gradient(#000 0% 25%, transparent 0% 50%);
  background-size: 20px 20px;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}

.qrcode-box::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 40px;
  height: 40px;
  background: #fff;
  border-radius: 5px;
}

.qrcode-box::before {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 20px;
  height: 20px;
  background: linear-gradient(45deg, #ff69b4, #8dd9ff);
  border-radius: 2px;
  z-index: 1;
}

.scan-line {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 2px;
  background: linear-gradient(90deg, transparent, #ff69b4, transparent);
  box-shadow: 0 0 10px #ff69b4;
  z-index: 2;
  animation: scanning 3s linear infinite;
}

@keyframes scanning {
  0% {
    top: 0;
  }
  50% {
    top: 100%;
  }
  100% {
    top: 0;
  }
}

.qrcode-tip {
  font-size: 14px;
  color: #666;
  margin-bottom: 5px;
}

.qrcode-refresh {
  font-size: 13px;
  color: #ff69b4;
  cursor: pointer;
}

.qrcode-refresh span:hover {
  text-decoration: underline;
}
</style>
